<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * MY_Form_Validation extends the built-in CI Form Validation Class
 *
 * This class adds new prepping and checking capabilities to the
 * built-in CI form_validation class.
 *
 * @author Casey McLaughlin
 * @package CIHeadStart
 * @link http://code.google.com/p/ciheadstart
 */
class MY_Form_validation extends CI_Form_validation
{
	/**
	 * Check for normal punctuation only
	 *
	 * This function checks to ensure that only normal punctuation used in names
	 * and simple texts exists in a string
	 *
	 * Allowed: . , ? ! @ # $ % & * ( ) " '
	 *
	 * @param string $str
	 * @return boolean
	 */
	function normal_punc_only($str)
	{
		$allowed = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '.', ',', '?', '!', '@', '#', '$', '%', '&', '*', '(', ')', '"', '\'');

		for ($x = 0; $x < strlen($str); $x++)
		{
			if ( ! in_array(substr($str, $x, 1), $allowed))
			{
				$this->set_message("normal_punc_only", "The %s must not contain any special punctuation or spaces.  Normal punctuation only.");
				return FALSE;
			}
		}

		//IF made it here
		return TRUE;
	}

	// --------------------------------------------------------------------

	/**
	 * Checks for normal punctuation only, also allow spaces
	 *
	 * This function behaves identically to normal_punc_only(), but also
	 * allows spaces
	 * @see normal_punc_only()
	 *
	 * @param string $str
	 * @return boolean
	 */
	function normal_punc_space($str)
	{
		$allowed = array(' ', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '.', ',', '?', '!', '@', '#', '$', '%', '&', '*', '(', ')', '"', '\'');

		for ($x = 0; $x < strlen($str); $x++)
		{
			if ( ! in_array(substr($str, $x, 1), $allowed))
			{
				$this->set_message("normal_punc_only", "The %s must not contain any special punctuation or spaces.  Normal punctuation only.");
				return FALSE;
			}
		}

		//IF made it here
		return TRUE;
	}

	// --------------------------------------------------------------------

	/**
	 * Check if string meets password rules
	 *
	 * This function allows you to define password rules and check to see
	 * if a string meets those rules
	 *
	 * @param string $str
	 * @return boolean
	 */
	function meets_password_rules($str)
	{
		if (strlen($str) < 6)
		{
			$this->set_message('meets_password_rules', "The %s must be at least six characters long!");
			return FALSE;
		}

		//If Made it here
		return TRUE;
	}

	// --------------------------------------------------------------------

	/**
	 * Checks to see if a date is formatted properly
	 *
	 * This function depends on the is_valid_date() function in the date helper
	 * @see is_valid_date()
	 *
	 * @param string $str
	 * @return boolean
	 */
	function valid_date($str)
	{
		$ci =& get_instance();
		$ci->load->helper('date');
		if (is_valid_date($str))
			return TRUE;
		else
		{
			$this->set_message('valid_date', "The %s field must be a valid date (eg. 04 May 1982)");
			return FALSE;
		}
	}

	// --------------------------------------------------------------------

	/**
	 * Checks to see if a string contains any HTML tags
	 *
	 * @param string $str
	 * @return boolean
	 */
	function notags($str)
	{
		if (strpos($str, "<") OR strpos($str, ">"))
		{
			$this->set_message('notags', "The %s field cannot contain any code!");
			return FALSE;
		}
		else
			return TRUE;
	}

	// --------------------------------------------------------------------

	/**
	 * Checks to see if a string contains a valid URL
	 *
	 * @author http://www.geekzilla.co.uk/View2D3B0109-C1B2-4B4E-BFFD-E8088CBC85FD.htm
	 * @param string $str
	 * @return boolean
	 */
	function valid_url($str)
	{
		$str = trim($str);

		$regex = "((https?|ftp|gopher|telnet|file|notes|ms-help):((//)|(\\\\))+[\w\d:#@%/;$()~_?\+-=\\\.&]*)";

		//Use preg_match_all instead of preg_match to ensure
		//that the string contains only ONE URL; not several.
		$temp = array();
		if (preg_match_all($regex, $str, $temp) === 1)
			return TRUE;
		else
		{
			$this->set_message('valid_url', "The %s field must be a valid URL!");
			return FALSE;
		}
	}

	// --------------------------------------------------------------------

	/**
	 * Checks to see if a string contains a specific value
	 * 
	 * If the string contains the value, return true, otherwise
	 * return false
	 *
	 * @param string $str
	 * @param string $val
	 * @return boolean
	 */
	function contains($str, $val)
	{
		if (strpos($str, $val) !== FALSE)
			return TRUE;
		else
		{
			$this->set_message('contains', "The %s field must contain '$val'");
			return FALSE;
		}
	}
}

/* End of file MY_Form_validation.php */
/* Location: ./app/libraries/MY_Form_validation.php */